from Tkinter import *
import tkFont
from tkFileDialog import askopenfilename

class ControlBlock:

  def loadNew(self):
    options={}
    options['initialfile'] = '*.csv'
    status = self.enabled
    self.enabled = False
    self.plantFile=askopenfilename(**options)
    if self.plantFile != '':
      #self.master.openPlantFile(self.plantFile)
      self.master.event_generate("<<OpenPlant>>",when="tail")
      return
    if status:
      self.enabled = True
      self.startTimer()


  def __init__(self, master):

    self.master = master

    self.expenses = 0
    self.cash = 0
    self.disableWarning = 0
    self.timerInterval = 1000
    self.day = 1
    self.hour = 1
    self.minute = 0
    self.dayMax = 5
    self.enabled = False

    self.frame = Frame(master, width = 200, height = 100)
    self.frame.pack()

    self.TimerPause = Button(self.frame, text="Start", command = self.pause)
    self.Faster = Button(self.frame, text = "+", command = self.faster)
    self.Slower = Button(self.frame, text = "-", command = self.slower)
    self.Reset = Button(self.frame, text = "Reset", command = self.reset)
    self.Schedule = Button(self.frame, text="Schedule") #, command = openSchedule)
    self.LoadNew = Button(self.frame, text="Load New File", command = self.loadNew)
    self.DisableWarning = Checkbutton(self.frame, text="Disable day warning",
        variable=self.disableWarning, font=tkFont.Font(size=8))
    self.Expenses = Label(self.frame, text = "Expenses: 0")
    self.Cash = Label(self.frame, text = "Cash: 0")
    self.CurrentDay = Label(self.frame, text = "Day: 0")
    self.CurrentTime = Label(self.frame, text = "Time: 0:00")

    self.TimerPause.place(relwidth=0.3, relx=0.15, relheight=0.2, rely=0.3,
        anchor=CENTER)
    self.Schedule.place(relwidth= 0.3, relx=0.15, relheight=0.2, rely=0.5,
        anchor=CENTER)
    self.Faster.place(relwidth=0.15, relx=0.075, relheight=0.2, rely=0.1,
        anchor=CENTER)
    self.Slower.place(relwidth=0.15, relx=0.225, relheight=0.2, rely=0.1,
        anchor=CENTER)
    self.Reset.place(relwidth=0.3, relx=0.15, relheight=0.2, rely=0.7,
        anchor=CENTER)
    self.LoadNew.place(relwidth=1.0, relx=0.5, relheight=0.2, rely=0.9,
        anchor=CENTER)
    self.Expenses.place(relwidth = 0.7, relx =.65, relheight = .16, rely=.08,
        anchor=CENTER)
    self.Cash.place(relwidth = 0.7, relx =.65, relheight = .16, rely=.24,
        anchor=CENTER)
    self.CurrentDay.place(relwidth = 0.7, relx =.65, relheight = .16, rely=.40,
        anchor=CENTER)
    self.CurrentTime.place(relwidth = 0.7, relx =.65, relheight = .16, rely=.56,
        anchor=CENTER)
    self.DisableWarning.place(relwidth = 0.7, relx =.65, relheight = .16, rely=.72,
        anchor=CENTER)

  def timer(self):
    self.updateTime()
    if self.enabled:
      self.startTimer()

  def updateTime(self):
    self.minute += 1
    if self.minute >= 60:
      self.hour += 1
      self.minute -= 60
      if self.hour >= 8:
        if self.day == self.dayMax:
          self.setTime(self.day, self.hour, self.minute)
          self.enabled = False
          return
        self.day += 1
        self.hour -= 7
    self.setTime(self.day, self.hour, self.minute)



  def setTime(self, day, hour, minute):
    self.CurrentDay.config(text="Day: {0}".format(day))
    self.CurrentTime.config(text="Time: {0}:{1:02}".format(hour, minute))

  def pause(self):
    if self.enabled:
      self.enabled = False
      self.TimerPause.config(text="Resume")
    else:
      self.enabled = True
      self.TimerPause.config(text="Pause")
      self.startTimer()

  def startTimer(self):
    self.master.after(self.timerInterval, self.timer)

  def faster(self):
    self.timerInterval /= 2

  def slower(self):
    self.timerInterval *= 2

  def reset(self):
    self.day = 1
    self.hour = 1
    self.minute = 0
    self.setTime(self.day, self.hour, self.minute)
